using System;

namespace Microsoft.SqlServer.Tools.PublishWizard.CommandLine
{
	public abstract class CommandLineParserBase
	{
		internal EventHandler<MessageEventArgs> ErrorMessage;

		private int _requiredDefaultCommands;

		internal abstract string Syntax
		{
			get;
		}

		protected int RequiredDefaultCommands
		{
			get
			{
				return this._requiredDefaultCommands;
			}
			set
			{
				this._requiredDefaultCommands = value;
			}
		}

		public bool Parse(string[] args, object destination)
		{
			return Parser.ParseArgumentsWithUsage(args, destination, this.Syntax) && this.CheckSyntax(destination);
		}

		public virtual bool CheckSyntax(object destination)
		{
			CommonCommandLineArguments commonCommandLineArguments = (CommonCommandLineArguments)destination;
			bool result = true;
			if (commonCommandLineArguments.ConnectionString != null)
			{
				if (commonCommandLineArguments.UserName != null || commonCommandLineArguments.Password != null || commonCommandLineArguments.LocalDatabaseName != null || commonCommandLineArguments.ServerName != null)
				{
					result = false;
					this.ShowErrorMessage(CommandLineResources.ConnStr);
				}
			}
			else
			{
				if (commonCommandLineArguments.LocalDatabaseName == null)
				{
					result = false;
					this.ShowErrorMessage(CommandLineResources.DatabaseNameRequired);
				}
				if (commonCommandLineArguments.Password != null && commonCommandLineArguments.UserName == null)
				{
					result = false;
					this.ShowErrorMessage(CommandLineResources.PasswordWithoutUser);
				}
				else if (commonCommandLineArguments.Password == null && commonCommandLineArguments.UserName != null)
				{
					result = false;
					this.ShowErrorMessage(CommandLineResources.UserWithoutPassword);
				}
			}
			if (commonCommandLineArguments.SchemaOnly && commonCommandLineArguments.DataOnly)
			{
				this.ShowErrorMessage(CommandLineResources.DataOnlyAndSchemaOnly);
				result = false;
			}
			if (!string.IsNullOrEmpty(commonCommandLineArguments.TargetServer) && commonCommandLineArguments.TargetServer != CommandLineResources.SQLServerVersion2000 && commonCommandLineArguments.TargetServer != CommandLineResources.SQLServerVersion2005 && commonCommandLineArguments.TargetServer != CommandLineResources.SQLServerVersion2008)
			{
				this.ShowErrorMessage(CommandLineResources.ERROR_InvalidTargetServerVersion);
				result = false;
			}
			return result;
		}

		protected void ShowErrorMessage(string message)
		{
			if (this.ErrorMessage != null)
			{
				this.ErrorMessage(this, new MessageEventArgs(message));
			}
		}
	}
}
